package com.hao.wms.mvc;

import com.alibaba.fastjson.JSON;
import com.hao.wms.domain.SystemMenu;
import com.hao.wms.query.SystemMenuQueryObject;
import com.hao.wms.service.ISystemMenuService;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SystemMenuAction extends BaseAction{
	/**
	 * 
	 */
	private ISystemMenuService systemMenuService;
	
	
	private SystemMenu systemMenu = new SystemMenu();
	
	public SystemMenu getSystemMenu() {
		return systemMenu;
	}
	public void setSystemMenu(SystemMenu systemMenu) {
		this.systemMenu = systemMenu;
	}
	private SystemMenuQueryObject queryObject = new SystemMenuQueryObject();
	
	
	public SystemMenuQueryObject getQueryObject() {
		return queryObject;
	}
	public void setQueryObject(SystemMenuQueryObject queryObject) {
		this.queryObject = queryObject;
	}
	public void setSystemMenuService(ISystemMenuService systemMenuService) {
		this.systemMenuService = systemMenuService;
	}
	@InputConfig(methodName = "input")
	@RequiredPermission(value="系统菜单列表")
	public String execute() {
		putContext("menus",systemMenuService.queryMenuByParentId(queryObject.getParentId()));
		putContext(PAGERESULT, systemMenuService.query(queryObject));
		
		return LIST;
	}
	@RequiredPermission(value="新增/修改系统菜单")
	public String input() {
		if(queryObject.getParentId()>0){
			putContext("parentName",systemMenuService.get(queryObject.getParentId()).getName());
		}else {
			putContext("parentName","根菜单");
		}
		if(systemMenu.getId()!=null) {
			systemMenu = systemMenuService.get(systemMenu.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改系统菜单")
	public String saveOrUpdate() {
		if(queryObject.getParentId()>0){
			SystemMenu parent = systemMenuService.get(queryObject.getParentId());
			systemMenu.setParent(parent);
		}
		try {
			if(systemMenu.getId()!=null) {
                systemMenuService.update(systemMenu);
                addActionMessage("修改成功");
            }else {
                systemMenuService.save(systemMenu);
                addActionMessage("保存成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	public void prepareSave(){
		if(systemMenu.getId()!=null) {
			systemMenu = systemMenuService.get(systemMenu.getId());
			
		}
	}
	@RequiredPermission(value="删除系统菜单")
	public String delete() throws IOException {
		try {
			systemMenuService.delete(systemMenu);
			putResponseText("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			putResponseText(e.getMessage());
		}
		return NONE;
	}
	public String loadMenusByParentSn() throws IOException {
		List<SystemMenu> menus = systemMenuService.queryMenuByParentSn(queryObject.getParentSn());
		List<Object> jsonList = new ArrayList<>();
		for (SystemMenu menu : menus) {
			jsonList.add(menu.toJson());
		}
		ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(JSON.toJSONString(jsonList));
		return NONE;
	}
	
	

}
